package com.example.security;

import com.example.security.bean.User;
import com.example.security.dao.UserDao;
import com.google.gson.Gson;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.scheduling.annotation.EnableAsync;

import java.security.NoSuchAlgorithmException;

@SpringBootApplication
@MapperScan
@EnableAsync
public class SecurityApplication {

  public static void main(String[] args) throws NoSuchAlgorithmException {

    ApplicationContext context = SpringApplication.run(SecurityApplication.class, args);
    UserDao userDao = context.getBean(UserDao.class);

    User user1 = userDao.getUserByName("王二");
    System.out.println(user1);

    SimpleMailMessage message = new SimpleMailMessage();
    message.setTo("abc@abc.com"); // 收件人
    message.setSubject("标题");
    message.setText("内容");
    message.setFrom("发信人");

    var rabbitTemplate = context.getBean(RabbitTemplate.class);

    var gson = context.getBean(Gson.class);
    // 注意: 发送给RabbitMq的对象必须实现了Serializable接口, 即可序列号

    // 发送方 指定: 1. exchange, 2. routingKey即可
    rabbitTemplate.convertAndSend("direct-ex", "morning-key", gson.toJson(user1));

    //    RedisTemplate<String, Object> redisTemplate =
    //        (RedisTemplate<String, Object>) context.getBean("redisTemplate");
    //    var ops = redisTemplate.boundValueOps("test");
    //    ops.set("aaaaaaaaaaaa");

    //    var mailSender = context.getBean(JavaMailSender.class);
    //
    //    mailSender.send(message);
  }
}
